/**
 * 
 */
package NewGW;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import utils.variable;


/**
 *
 */
public class InitSerialCon
	{
	/**
	 * Variables
	 */
	
	
	/**
	 * Constructeur
	 */
	public InitSerialCon()
		{
		
		}
	
	public static CommPort connect (String portName) throws Exception
	    {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if (portIdentifier.isCurrentlyOwned())
        	{
            System.out.println("Error: Port is currently in use");
            throw new Exception("Error: Port is currently in use");
        	}
        else
        	{
            //CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
        	CommPort commPort = portIdentifier.open(portName,2000);
        	
            if(commPort instanceof SerialPort)
            	{
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN);
                
                serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                
                variable.setSerialIn(serialPort.getInputStream());
                variable.setSerialOut(serialPort.getOutputStream());
                return commPort;
            	}
            else
            	{
                System.out.println("Error: Only serial ports can be handled");
                throw new Exception("Error: Only serial ports can be handled");
            	}
        	}
	    }
	
	
	/*2012*//*RATEL Alexandre 8)*/
	}
